package com.itheima.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 偏爱即浪漫
 * 返回通用结果类，服务器响应的数据都会封装成这个类
 */
@Data
@ApiModel("返回结果")
public class Result<T> implements Serializable {
    @ApiModelProperty("编码")
    private Integer code;
    @ApiModelProperty("错误信息")
    private String msg;
    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty("动态数据")
    private Map map = new HashMap();
    //成功方法
    public static <T> Result<T> success(T object){
        Result<T> result = new Result<T>();
        result.code = 1;
        result.data = object;
        return result;
    }
    //失败方法
    public static <T> Result<T> error(String  msg){
        Result result = new Result();
        result.code = 0;
        result.msg = msg;
        return result;
    }
    //添加方法
    public Result<T> add(String key,String value){
        this.map.put(key,value);
        return this;
    }
}
